package com.dengqi.mymodule.activity.glide.activity;


import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

/**
 * 解决 viewPager.setOffscreenPageLimit(0);无效问题
 * 防止viewpager预加载
 * Created by dell on 2019/2/12
 */
public abstract class PreLoadFragment extends Fragment {
    private boolean mIsDataInited;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        initView(view);
        initListener();
        if (!mIsDataInited) {
            if (getUserVisibleHint()) {
                initData();
                mIsDataInited = true;
            }
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        //防止数据预加载, 只预加载View，不预加载数据
        if (isVisibleToUser && isVisible() && !mIsDataInited) {
            initData();
            mIsDataInited = true;
        }
    }

    public abstract void initView(View view);

    public abstract void initListener();

    public abstract void initData();
}
